package com.kellycheung.chicken_go_go;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;

public class GMediaPlayer {
	public enum GMPType {
		bgMusic(0,"gpBgSound","gpBgSoundMuted",true) , soundEffect(1,"gpEffectSound","gpEffectSoundMuted",false)
		;
		
		private final int iVal;
		private final String sVolumnPerf; 
		private final String sMutedPerf;
		private final boolean bLoop;
		
		private GMPType( int p_iVal , String p_sVolumnPerf , String p_sMutedPerf , boolean p_bLoop ) {
			this.iVal = p_iVal;
			this.sVolumnPerf = p_sVolumnPerf;
			this.sMutedPerf = p_sMutedPerf;
			this.bLoop = p_bLoop;
		}
		
		public int getValue() {
			return this.iVal;
		}
		
		public String getVolumnPrefStr() {
			return this.sVolumnPerf;
		}
		
		public String getMutedPrefStr() {
			return this.sMutedPerf;
		}
		
		public boolean getDefaultLoop() {
			return this.bLoop;
		}
	}
	
	public class GMPVolumn extends Thread  {
		protected GMediaPlayer gmpPlayer;
		protected MediaPlayer mpMediaPlayer;
		protected int iRes;
		protected GMPType gmptType;
		protected int iVolumn;
		protected int iMuted;
		
		public GMPVolumn( GMediaPlayer p_gmpPlayer , GMPType p_gmptType , int[] p_iArr ) {
			try {
				this.gmpPlayer = p_gmpPlayer;
				this.iRes = 0;
				this.gmptType = p_gmptType;
				this.iVolumn = p_iArr[0];
				this.iMuted = p_iArr[1];
			} catch ( Exception p_e ) {
				// TODO Auto-generated catch block
				GameUtil.LogMsg( this , "GMPVolumn" );
				GameUtil.ErrorHandler( this , p_e );
			}
		}
		
		public void destroy() {
			try {
				if ( this.mpMediaPlayer != null ) {
					this.mpMediaPlayer.stop();
					this.mpMediaPlayer = null;
				}
				
				this.gmpPlayer = null;
			} catch ( Exception p_e ) {
				// TODO Auto-generated catch block
				GameUtil.LogMsg( this , "GMPVolumn.destroy" );
				GameUtil.ErrorHandler( this , p_e );
			}
		}
		
		public void playerStart() {
		}
		
		public void playerPause() {
			try {
				this.mpMediaPlayer.pause();
			} catch ( Exception p_e ) {
				// TODO Auto-generated catch block
				GameUtil.LogMsg( this , "GMPVolumn.playerPause" );
				GameUtil.ErrorHandler( this , p_e );
			}
		}
		
		public void playerStop() {
			try {
				this.mpMediaPlayer.stop();
			} catch ( Exception p_e ) {
				// TODO Auto-generated catch block
				GameUtil.LogMsg( this , "GMPVolumn.playerStop" );
				GameUtil.ErrorHandler( this , p_e );
			}
		}
		
		public void playerDestroy() {
			try {
				this.playerStop();
				this.mpMediaPlayer = null;
			} catch ( Exception p_e ) {
				// TODO Auto-generated catch block
				GameUtil.LogMsg( this , "GMPVolumn.playerDestroy" );
				GameUtil.ErrorHandler( this , p_e );
			}
		}
		
		public void playMusic( int p_iRes ) {
			try {
				if ( this.iRes != p_iRes ) {
			        this.iRes = p_iRes;
			        
					if ( this.mpMediaPlayer != null ) {
						if ( this.mpMediaPlayer.isPlaying() ) {
							this.mpMediaPlayer.stop();
							this.mpMediaPlayer = null;
						}
					}
					
		        	if ( this.iRes != 0 ) {
				        AssetFileDescriptor afd = this.gmpPlayer.geMain.aApp.getResources().openRawResourceFd( this.iRes );
				        this.mpMediaPlayer = new MediaPlayer();
						this.mpMediaPlayer.setDataSource( afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength() );
						this.mpMediaPlayer.prepareAsync();
						this.mpMediaPlayer.setOnCompletionListener( new OnCompletionListener() {
							@Override
							public void onCompletion(MediaPlayer mp) {
								// TODO Auto-generated method stub
								if ( !mpMediaPlayer.isPlaying() ) {
									mpMediaPlayer.start();
									setVolume( iVolumn );
								}
							}
						});
						afd.close();
		        	}
				}
				else {
					this.resumeMusic( p_iRes );
				}
			} catch ( Exception p_e ) {
				// TODO Auto-generated catch block
				GameUtil.LogMsg( this , "GMPVolumn.playMusic" );
				GameUtil.ErrorHandler( this , p_e );
			}
		}
		
		public void resumeMusic( int p_iRes ) {
			try {
				if ( this.mpMediaPlayer != null ) {
					if ( this.iRes == p_iRes ) {
						if ( !this.mpMediaPlayer.isPlaying() ) {
							this.mpMediaPlayer.start();
							this.setVolume( this.iVolumn );
						}
					}
				}
			} catch ( Exception p_e ) {
				// TODO Auto-generated catch block
				GameUtil.LogMsg( this , "GMPVolumn.resumeMusic" );
				GameUtil.ErrorHandler( this , p_e );
			}
		}
		
		public void pauseMusic( int p_iRes ) {
			try {
				if ( this.mpMediaPlayer != null ) {
					if ( this.iRes == p_iRes ) {
						if ( this.mpMediaPlayer.isPlaying() ) {
							this.mpMediaPlayer.pause();
						}
					}
				}
			} catch ( Exception p_e ) {
				// TODO Auto-generated catch block
				GameUtil.LogMsg( this , "GMPVolumn.pauseMusic" );
				GameUtil.ErrorHandler( this , p_e );
			}
		}
		
		public void stopMusic( int p_iRes ) {
			try {
				if ( this.mpMediaPlayer != null ) {
					if ( this.iRes == p_iRes ) {
						if ( this.mpMediaPlayer.isPlaying() ) {
							this.mpMediaPlayer.stop();
						}
					}
				}
			} catch ( Exception p_e ) {
				// TODO Auto-generated catch block
				GameUtil.LogMsg( this , "GMPVolumn.stopMusic" );
				GameUtil.ErrorHandler( this , p_e );
			}
		}
		
		public void setVolume( int p_iVal ) {
			try {
				if ( p_iVal < 0 ) {
					p_iVal = 0;
				}
				else if ( p_iVal > 100 ) {
					p_iVal = 100;
				}
				
				if ( this.mpMediaPlayer != null ) {
					if ( this.iMuted == 0 ) {
						this.mpMediaPlayer.setVolume( (float)p_iVal / 100.0f , (float)p_iVal / 100.0f );
					}
					else {
						this.mpMediaPlayer.setVolume( 0f , 0f );
					}
				}
				
				this.iVolumn = p_iVal;
				
				this.gmpPlayer.geMain.gpPref.setPerference( this.gmptType.sVolumnPerf , this.iVolumn );
			} catch ( Exception p_e ) {
				// TODO Auto-generated catch block
				GameUtil.LogMsg( this , "GMPVolumn.setVolume" );
				GameUtil.ErrorHandler( this , p_e );
			}
		}
		
		public void mute() {
			try {
				if ( this.iMuted == 1 ) {
					this.iMuted = 0;
				}
				else {
					this.iMuted = 1;
				}
				
				this.gmpPlayer.geMain.gpPref.setPerference( this.gmptType.sMutedPerf , this.iMuted );
			} catch ( Exception p_e ) {
				// TODO Auto-generated catch block
				GameUtil.LogMsg( this , "GMPVolumn.mute" );
				GameUtil.ErrorHandler( this , p_e );
			}
		}
		
		public void mute( boolean p_bMute ) {
			try {
				if ( p_bMute ) {
					this.iMuted = 1;
				}
				else {
					this.iMuted = 0;
				}
				
				this.gmpPlayer.geMain.gpPref.setPerference( this.gmptType.sMutedPerf , this.iMuted );
			} catch ( Exception p_e ) {
				// TODO Auto-generated catch block
				GameUtil.LogMsg( this , "GMPVolumn.mute(boolean)" );
				GameUtil.ErrorHandler( this , p_e );
			}
		}
		
		public boolean isMute() {
			return ( this.iMuted == 0 ? false : true );
		}
		
		public int getVolume() {
			return this.iVolumn;
		}
	}
	
	protected GameEngine geMain;
	protected GMPVolumn[] gmpvVol;
	
	public GMediaPlayer( GameEngine p_ge ) {
		try {
			this.geMain = p_ge;
			this.gmpvVol = new GMPVolumn[2];
			
			this.gmpvVol[ GMPType.bgMusic.getValue() ] = new GMPVolumn( this , GMPType.bgMusic , this.getVolumnFromPref( GMPType.bgMusic ) );
			this.gmpvVol[ GMPType.bgMusic.getValue() ].run();
			
			this.gmpvVol[ GMPType.soundEffect.getValue() ] = new GMPVolumn( this , GMPType.soundEffect , this.getVolumnFromPref( GMPType.soundEffect ) );
			this.gmpvVol[ GMPType.soundEffect.getValue() ].run();
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GMediaPlayer" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public void destroy() {
		try {
			if ( this.gmpvVol != null ) {
				for ( GMPVolumn gmpv : this.gmpvVol ) {
					gmpv.destroy();
				}
				
				this.gmpvVol = null;
			}
			
			this.geMain = null;
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GMediaPlayer.destroy" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public void playMusic( GMPType p_gmpType , int p_iRes ) {
		try {
			if ( this.gmpvVol[ p_gmpType.getValue() ] != null ) {
				this.gmpvVol[ p_gmpType.getValue() ].playMusic( p_iRes );
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GMediaPlayer.playMusic" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public void stopMusic( GMPType p_gmpType , int p_iRes ) {
		try {
			if ( this.gmpvVol[ p_gmpType.getValue() ] != null ) {
				this.gmpvVol[ p_gmpType.getValue() ].playMusic( p_iRes );
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GMediaPlayer.stopMusic" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
 
	public void pauseMusic( GMPType p_gmpType , int p_iRes ) {
		try {
			if ( this.gmpvVol[ p_gmpType.getValue() ] != null ) {
				this.gmpvVol[ p_gmpType.getValue() ].pauseMusic( p_iRes );
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GMediaPlayer.pauseMusic" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public void resumeMusic( GMPType p_gmpType , int p_iRes ) {
		try {
			if ( this.gmpvVol[ p_gmpType.getValue() ] != null ) {
				this.gmpvVol[ p_gmpType.getValue() ].resumeMusic( p_iRes );
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GMediaPlayer.resumeMusic" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public int[] getVolumnFromPref( GMPType p_gmpType ) {
        int[] iReturn = null;
        
		try {
			int iVolumn = this.geMain.gpPref.getPerference( p_gmpType.getVolumnPrefStr() , 100 );
	        int iMuted = this.geMain.gpPref.getPerference( p_gmpType.getMutedPrefStr() , 0 );
	        int iVolReal = 0;
	        
	        iReturn = new int[]{ iVolumn , iMuted };
	    	
	    	if ( iMuted == 1 ) {
	    		iVolReal = 0;
	    	}
	    	else {
	    		iVolReal = iVolumn;
	    	}
	    	
	    	if ( this.gmpvVol[ p_gmpType.getValue() ] != null ) {
				this.setVolume( p_gmpType , iVolReal );
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GMediaPlayer.getVolumnFromPref" );
			GameUtil.ErrorHandler( this , p_e );
		}
        
        return iReturn;
	}
	
	public void setVolume( GMPType p_gmpType , int p_iVal ) {
		try {
			if ( this.gmpvVol[ p_gmpType.getValue() ] != null ) {
				this.gmpvVol[ p_gmpType.getValue() ].setVolume( p_iVal );
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GMediaPlayer.getVolumnFromPref" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public void addVolume( GMPType p_gmpType , int p_iVal ) {
		try {
			if ( this.gmpvVol[ p_gmpType.getValue() ] != null ) {
				this.gmpvVol[ p_gmpType.getValue() ].mute( false );
				this.gmpvVol[ p_gmpType.getValue() ].setVolume( this.gmpvVol[ p_gmpType.getValue() ].iVolumn + p_iVal );
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GMediaPlayer.addVolume" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public void mute( GMPType p_gmpType ) {
		try {
			if ( this.gmpvVol[ p_gmpType.getValue() ] != null ) {
				this.gmpvVol[ p_gmpType.getValue() ].mute();
				this.gmpvVol[ p_gmpType.getValue() ].setVolume( this.gmpvVol[ p_gmpType.getValue() ].iVolumn );
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GMediaPlayer.mute(GMPType)" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public void mute( GMPType p_gmpType , boolean p_bMute ) {
		try {
			if ( this.gmpvVol[ p_gmpType.getValue() ] != null ) {
				this.gmpvVol[ p_gmpType.getValue() ].mute( p_bMute );
				this.gmpvVol[ p_gmpType.getValue() ].setVolume( this.gmpvVol[ p_gmpType.getValue() ].iVolumn );
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GMediaPlayer.mute(GMPType,Boolean)" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public boolean isMute( GMPType p_gmpType ) {
		try {
			if ( this.gmpvVol[ p_gmpType.getValue() ] != null ) {
				return this.gmpvVol[ p_gmpType.getValue() ].isMute();
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GMediaPlayer.isMute" );
			GameUtil.ErrorHandler( this , p_e );
		}
		
		return false;
	}
	
	public int getVolume( GMPType p_gmpType ) {
		try {
			if ( this.gmpvVol[ p_gmpType.getValue() ] != null ) {
				GameUtil.LogMsg(this, "volumn get now "+this.gmpvVol[ p_gmpType.getValue() ].getVolume());
				return this.gmpvVol[ p_gmpType.getValue() ].getVolume();
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GMediaPlayer.getVolume" );
			GameUtil.ErrorHandler( this , p_e );
		}
		
		return 0;
	}
}
